package com.lemo.hibernate;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Date;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.jdbc.Work;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class HibernateDemo2 {

	SessionFactory sessionFactory = null;
	Session session = null;
	Transaction transaction = null;

	@Before
	public void init() {
		Configuration configuration = new Configuration().configure();
		ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties())
				.buildServiceRegistry();
		sessionFactory = configuration.buildSessionFactory(serviceRegistry);
		session = sessionFactory.openSession();
		transaction = session.beginTransaction();
	}

	@After
	public void destroy() {
		transaction.commit();
		session.close();
		sessionFactory.close();
	}

	@Test
	public void testSessionCache() {
		News news = (News) session.get(News.class, 1);
		System.out.println(news);

		News news1 = (News) session.get(News.class, 1);
		System.out.println(news1);
		News news2 = (News) session.get(News.class, 1);
		System.out.println(news2);
		News news3 = (News) session.get(News.class, 1);
		System.out.println(news3);
	}

	// 使session 与 database 同步
	@Test
	public void testSessionFlush() {
		News news = (News) session.get(News.class, 1);
		System.out.println(news);
		session.flush();
	}

	// 使database 与 session 同步
	@Test
	public void testSessionRefresh() {
		News news = (News) session.get(News.class, 1);
		System.out.println(news);
		session.refresh(news);
		System.out.println(news);
	}

	@Test
	public void testSessionClear() {
		News news = (News) session.get(News.class, 1);
		session.clear();
		News news1 = (News) session.get(News.class, 1);
	}

	@Test
	public void testSave() {
		News news = new News();
		news.setAuthor("aa");
		news.setAuthor("AA");
		news.setDate(new Date());
		System.out.println(news);
		session.save(news);
		System.out.println(news);

	}

	@Test
	public void testPersist() {
		News news = new News();
		news.setAuthor("bb");
		news.setAuthor("BB");
		news.setDate(new Date());
		System.out.println(news);
		session.save(news);
		System.out.println(news);
	}

	@Test
	public void testGet() {
		News news = (News) session.get(News.class, 1);
		System.out.println(news);
	}

	@Test
	public void testLoad() {
		News news = (News) session.get(News.class, 1);
		System.out.println(news);
	}

	@Test
	public void testUpdate() {
		News news = (News) session.get(News.class, 2);
		news.setAuthor("leeee");
		session.update(news);
		System.out.println(news);
	}
	
	@Test
	public void testSaveOrUpdate() {
		News news = (News) session.get(News.class, 1);
		news.setAuthor("leeee2");
		session.saveOrUpdate(news);
		System.out.println(news);
	}
	
	@Test
	public void testDelete() {
		News news = (News) session.get(News.class, 1);
		session.delete(news);
	}
	//从session缓存中 移除此对象
	@Test
	public void testEvict() {
		News news = (News) session.get(News.class, 2);
		session.evict(news);
		System.out.println(news);
	}
	//存储过程
	@Test
	public void testWork() {
		session.doWork(new Work() {
			
			@Override
			public void execute(Connection connection) throws SQLException {
				int holdability = connection.getHoldability();
				System.out.println(connection);
			}
		});
	}
	// 只更新你操作的字段
	@Test
	public void testDynamicUpdate() {
		News news = (News) session.get(News.class,4);
		news.setAuthor("6ggggggg");
		session.save(news);
	}
	// 只插入你操作的字段
	@Test
	public void testDynamicInsert() {
		News news = new News();
		news.setAuthor("BB3");
		news.setDate(new Date());
		session.save(news);
	}
	@Test
	public void test() {

		// 1. 创建一个 SessionFactory 对象
		SessionFactory sessionFactory = null;

		// 1). 创建 Configuration 对象: 对应 hibernate 的基本配置信息和 对象关系映射信息
		Configuration configuration = new Configuration().configure();

		// 4.0 之前这样创建
		// sessionFactory = configuration.buildSessionFactory();

		// 2). 创建一个 ServiceRegistry 对象: hibernate 4.x 新添加的对象
		// hibernate 的任何配置和服务都需要在该对象中注册后才能有效.
		ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties())
				.buildServiceRegistry();

		// 3).
		sessionFactory = configuration.buildSessionFactory(serviceRegistry);

		// 2. 创建一个 Session 对象
		Session session = sessionFactory.openSession();

		// 3. 开启事务
		Transaction transaction = session.beginTransaction();

		// 4. 执行保存操作
		News news = new News("Java12345", "ATGUIGU", new Date(new java.util.Date().getTime()));
		session.save(news);

		// 5. 提交事务
		transaction.commit();

		// 6. 关闭 Session
		session.close();

		// 7. 关闭 SessionFactory 对象
		sessionFactory.close();
	}

}
